﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH;
using Module.Category.BL;
using QLBH.DTO;

namespace Module.Category
{
    public partial class ProviderManagement : ViewBase
    {
        private static ProviderManagement instance;

        public static ProviderManagement Instance
        {
            get 
            {
                if (instance==null)
                {
                    instance = new ProviderManagement();
                }
                return ProviderManagement.instance; 
            }
            
        }
        public ProviderManagement()
            :base()
        {
            InitializeComponent();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AddProvider addpro = new AddProvider();
                if (addpro.ShowDialog()==DialogResult.OK)
                {
                    //ProviderManagementBL.Instance.AddProvider(addpro.Provider);
                    dataGridViewX1.DataSource = ProviderManagementBL.Instance.GetAllProviders();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void ProviderManagement_Load(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.DataSource = ProviderManagementBL.Instance.GetAllProviders();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditProvider();
        }
        private void EditProvider()
        {
            try
            {
                EditProvider editpro = new EditProvider();
                editpro.ProviderEdit = dataGridViewX1.CurrentRow.DataBoundItem as NhaCCDTO;

                if (editpro.ShowDialog() == DialogResult.OK)
                {
                    ProviderManagementBL.Instance.UpdateProvider(editpro.ProviderEdit);

                    dataGridViewX1.DataSource = ProviderManagementBL.Instance.GetAllProviders();

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditProvider();
        }

        private void dataGridViewX1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridViewX1.RowCount == 0)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
            }
            else
            {
                buttonEdit.Enabled = true;
                buttonDelete.Enabled = true;
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.DataSource = ProviderManagementBL.Instance.GetAllProviders();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<NhaCCDTO> selectedObjs = new List<NhaCCDTO>();
                foreach (DataGridViewRow row in dataGridViewX1.SelectedRows)
                {
                    selectedObjs.Add(row.DataBoundItem as NhaCCDTO);
                }

                if (selectedObjs.Count > 0)
                {
                    ProviderManagementBL.Instance.DeleteProviders(selectedObjs);

                    dataGridViewX1.DataSource = ProviderManagementBL.Instance.GetAllProviders();

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void tableLayoutPanel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
